package cn.dansj.common.utils.threadLocal;

import java.util.Objects;
import java.util.function.Supplier;

public class InheritThreadLocal<T> extends InheritableThreadLocal<T> {
    public static <S> InheritThreadLocal<S> withInitial(Supplier<? extends S> supplier) {
        return new SuppliedThreadLocal<>(supplier);
    }

    static final class SuppliedThreadLocal<T> extends InheritThreadLocal<T> {
        private final Supplier<? extends T> supplier;

        SuppliedThreadLocal(Supplier<? extends T> supplier) {
            this.supplier = Objects.requireNonNull(supplier);
        }

        @Override
        protected T initialValue() {
            return supplier.get();
        }
    }
}
